/*
 * Copyright 2011 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.bize.knexions.android.knexion.twitter;

import java.nio.charset.MalformedInputException;

import nl.bize.knexions.android.knexion.Storable;

/**
 * The storable Twitter account. Used to persist login data for an account.
 * 
 * @author Joost A. Bloemsma
 */
public class TwitterAccount implements Storable{
	public static final String CTX = "StorableTwitterAccount";
	private static final String TOKEN1 = "#1:";
	private static final String TOKEN2 = "#2:";
	private String accessToken1;
	private String accessToken2;
	private String userName;
	
	public TwitterAccount() {
		super();
	}
	
	public TwitterAccount(String userName) {
		super();
		this.setUserName(userName);
		//new TwitterAccountStore(AndroidKnexionsApplication.getAppContext()).
	}
		
	/**
	 * @return the userName
	 */
	final public String getUserName() {
		return this.userName;
	}
	/**
	 * @param userName the userName to set
	 */
	final void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * @return the accessToken1
	 */
	final String getAccessToken1() {
		return accessToken1;
	}
	/**
	 * @param accessToken1 the accessToken1 to set
	 */
	final void setAccessToken1(String accessToken1) {
		this.accessToken1 = accessToken1;
	}
	/**
	 * @return the accessToken2
	 */
	final String getAccessToken2() {
		return accessToken2;
	}
	/**
	 * @param accessToken2 the accessToken2 to set
	 */
	final void setAccessToken2(String accessToken2) {
		this.accessToken2 = accessToken2;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public String getData() {
		return TOKEN1 + this.accessToken1 + TOKEN2 + this.accessToken2;
	}
	/**
	 * {@inheritDoc}
	 */
	public String getName() {
		return this.getUserName();
	}
	/**
	 * {@inheritDoc}
	 */
	public void setName(String name) {
		this.setUserName(name);
	}
	/**
	 * {@inheritDoc}
	 * @throws MalformedInputException 
	 */
	public void setData(String data) throws MalformedInputException {
		if (data == null) {
			setAccessToken1(null);
			setAccessToken2(null);
		}
		if (!data.startsWith(TOKEN1)) throw new MalformedInputException(data.length());
		if (data.indexOf(TOKEN2) < TOKEN1.length()) throw new MalformedInputException(data.length());
		
		data = data.substring(TOKEN1.length());
		this.setAccessToken1(data.substring(0, data.indexOf(TOKEN2)));
		this.setAccessToken2(data.substring(data.indexOf(TOKEN2) + TOKEN2.length()));
	}

	/**
	 * {@inheritDoc}
	 */
	public String getContext() {
		return CTX;
	}
	
}

